package com.hit.server.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hit.server.api.entity.SysCustomerEntity;
import com.hit.server.api.dto.SysCustomerQueryDTO;
import com.hit.server.api.dto.SysCustomerSaveDTO;
import com.hit.server.api.dto.SysCustomerUpdateDTO;
import com.hit.server.api.vo.SysCustomerVO;
import com.hit.server.biz.mapper.SysCustomerMapper;
import com.hit.server.api.service.SysCustomerService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

/**
 * 客户表
 *
 * @author kehanjiang
 * @date 2025-04-03 17:25:40
 */
@Service
@RequiredArgsConstructor
public class SysCustomerServiceImpl extends ServiceImpl<SysCustomerMapper, SysCustomerEntity> implements SysCustomerService {

	private SysCustomerVO convertToVO(Object o) {
        if (o instanceof SysCustomerEntity) {
            SysCustomerVO vo = new SysCustomerVO();
            BeanUtil.copyProperties(o, vo);
			//设置其他参数
			
            return vo;
        }
        return null;
    }
	
	private void preCheck(SysCustomerEntity entity, Boolean update) {
        Long id = entity.getId();
        //String name = entity.getName();
        if (update) {//更新操作
           Assert.notNull(id, "id不可为空！");
        } else {
           //Assert.hasText(name, "name不可为空！");
        }
    }

    @Override
    public Page<SysCustomerVO> getSysCustomerPage(Page page, SysCustomerQueryDTO dto){
        SysCustomerEntity entity = new SysCustomerEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setName(null);
        final String name = dto.getName();
        LambdaQueryWrapper<SysCustomerEntity> wrapper = new LambdaQueryWrapper<>(entity);
        wrapper.like(StringUtils.hasText(name), SysCustomerEntity::getName, name);
        wrapper.orderByAsc(SysCustomerEntity::getId);
        return (Page<SysCustomerVO>) this.page(page, wrapper)
                .convert(this::convertToVO);
    }
	
    @Override
    public SysCustomerVO getSysCustomerById(Long id){
        Assert.notNull(id, "id不可为空！");
        return convertToVO(this.getById(id));
    }
	
	
    @Override
    public Boolean saveSysCustomer(SysCustomerSaveDTO dto){
        SysCustomerEntity entity = new SysCustomerEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, false);
        return this.save(entity);
    }
	
    @Override
    public Boolean updateSysCustomerById(SysCustomerUpdateDTO dto){
        SysCustomerEntity entity = new SysCustomerEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, true);
        return this.updateById(entity);
    }
	
    @Override
    public Boolean removeSysCustomerBatchByIds(List<Long> ids){
        Assert.notEmpty(ids, "ID不可为空！");
        return this.removeBatchByIds(ids);
    }
}